package cate.game.play.proce.play;

import cate.common.game.GameCollection;
import cate.common.table.d.GDPlay;
import cate.game.play.part.PlaySide;
import cate.game.play.part.TeamVS;
import cate.game.play.proce.round.RoundPO;
import cate.game.util.GameKit;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import easy.java.struct.ref.LongRef;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@NoteClass("战斗剧本PO")
@Document(collection = GameCollection.NAME_PLAY)
public class PlayPO {

    public static final String AUTO_DELETE_FIELD = "expiredDate";

    @NoteField("唯一id")
    @Id
    public String uid = GameKit.uid();

    @NoteField("剧本故事")
    public PlayStory story = new PlayStory();

    @NoteField("最高回合数")
    public int roundMax;

    @NoteField("A方信息")
    public PlaySide a;

    @NoteField("B方信息")
    public PlaySide b;

    @NoteField("回合数据")
    public List<RoundPO> rounds = new ArrayList<>();

    @NoteField("队伍的开局状态记录")
    public TeamVS teamBegin;

    @NoteField("队伍的结束状态记录")
    public TeamVS teamEnd;

    @NoteField("胜利方")
    public byte winner;

    @NoteField("开始播放的时间戳")
    public long startTime;

    @NoteField("结束播放的时间戳")
    public long endTime;

    @NoteField("剧本过期时间")
    public LocalDateTime expiredDate;

    /** 精简优化剧本 */
    public void streamline() {
        for (RoundPO round : rounds) {
            round.streamline();
        }
    }

    /** 获取播放时长 */
    public long playTime() {
        LongRef cost = new LongRef(GDPlay.PlayTime.ENTER);
        rounds.forEach(
                round -> round.actions.forEach(
                        action -> {
                            if(action.playTime!=null){
                                cost.v += action.playTime;
                            }
                        }));
        return cost.v;
    }
}
